//
//  JGWorkSwitchView.m
//  JGRider
//
//  Created by 单启志 on 2018/3/1.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGWorkSwitchView.h"

@interface JGWorkSwitchView()
@property (weak, nonatomic) IBOutlet UILabel *statusLab;

@property (weak, nonatomic) IBOutlet UISwitch *workSwitch;
@property (weak, nonatomic) IBOutlet UIImageView *bgImageV;



@end
@implementation JGWorkSwitchView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
        self.frame=frame;

        [self configSubViews];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self configSubViews];
}

-(void)configSubViews{

    self.workSwitch.offImage=[UIImage imageNamed:@"switch_off"];
    self.workSwitch.onImage=[UIImage imageNamed:@"switch_on"];

    FQWeakSelf(self);
    [self addTapGestureRecognizer:^(UITapGestureRecognizer *recognizer, NSString *gestureId) {
        [UIView animateWithDuration:0.25 animations:^{
            weakself.alpha=0;
        } completion:^(BOOL finished) {
            weakself.hidden=YES;
            weakself.alpha=1;
        }];

    }];
}

- (IBAction)onSwitchChanged:(UISwitch*)sender {

    if ([self.delegate respondsToSelector:@selector(workSwitchWithStatus:)]) {
        [self.delegate workSwitchWithStatus:sender.isOn];
    }

    self.isWorking=sender.isOn;

    [UIView animateWithDuration:0.25 delay:0.25 options:UIViewAnimationOptionTransitionNone animations:^{
        self.alpha=0;
    } completion:^(BOOL finished) {
        self.hidden=YES;
        self.alpha=1;
    }];
//    [UIView animateWithDuration:0.25 animations:^{
//        self.alpha=0;
//    } completion:^(BOOL finished) {
//        self.hidden=YES;
//        self.alpha=1;
//    }];
}

-(void)setIsWorking:(BOOL)isWorking{

    if (isWorking==YES) {
        self.statusLab.text=@"工作中";
        self.bgImageV.image=[UIImage imageNamed:@"rest_icon_state_work"];
    }else{
        self.statusLab.text=@"休息中";
        self.bgImageV.image=[UIImage imageNamed:@"rest_icon_state_rest"];
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
}

@end
